//
//  NetworkTools+Extension.swift
//  Practice
//
//  Created by miaozhihui on 2017/1/16.
//  Copyright © 2017年 DeKuTree. All rights reserved.
//

import Foundation

// MARK: - 封装新浪微博的网络请求方法
extension NetworkTools {
    
    /// 加载微博数据字典数组
    ///
    /// - Parameters:
    ///   - since_id: 返回ID比since_id大的微博，默认为0
    ///   - max_id: 返回ID比since_id小或者相等的微博，默认为0
    ///   - finish: 完成回调
        func statusList(since_id: Int64 = 0, max_id: Int64 = 0, finish:@escaping (_ list:[[String:Any]]?,_ isSuccess:Bool)->()) {
            
            let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
            
            let params = ["since_id":since_id,"max_id":max_id > 0 ? max_id-1 : 0]
            
            tokenRequest(method: .GET, URLString: urlString, parameters: params) { (json, error) in
                let result = (json as? [String:Any])?["statuses"] as? [[String:Any]]
                finish(result, error == nil)
        }
    }
    
    /// 获取微博未读数量
    ///
    /// - Parameter completion: 完成回调[未读消息数]
    func unreadMessage(completion:@escaping (_ count:Int)->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, error) in
            
            let result = json as? [String:Any]
            let count = result?["status"] as? Int
            completion(count ?? 0)
        }
    }
}
